package io.renren.modules.biz.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.RedisUtils;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.biz.entity.MemberEntity;
import io.renren.modules.biz.entity.RoomEntity;
import io.renren.modules.biz.service.MemberService;
import io.renren.modules.biz.service.RoomService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * 会员表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-09-03 14:51:31
 */
@RestController
@RequestMapping("biz/member")
public class MemberController {
	@Autowired
	private MemberService memberService;

	@Autowired
	private RedisUtils redisUtils;

	@Autowired
	private RoomService roomService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("biz:member:list")
	@SuppressWarnings("unchecked")
	public R list(@RequestParam Map<String, Object> params) {
		// 仅在线会员
		if ("0".equals(params.get("online"))) {
			Set<String> keys = redisUtils.keys("ONLINE_USER_*");
			List<Long> ids = new ArrayList<Long>(keys == null ? 1 : keys.size());
			if (CollectionUtils.isNotEmpty(keys)) {
				for (String key : keys) {
					ids.add(Long.valueOf(key.substring(12)));
				}
			} else {
				ids.add(0L);
			}
			params.put("ids", ids);
		}
		PageUtils page = memberService.queryPage(params);
		List<MemberEntity> list = (List<MemberEntity>) page.getList();
		if (CollectionUtils.isNotEmpty(list)) {
			for (MemberEntity memberEntity : list) {
				memberEntity.setOnline(redisUtils.get("ONLINE_USER_" + memberEntity.getId()) != null);
				if (memberEntity.getOnline()) {
					RoomEntity room = roomService.selectOne(new EntityWrapper<RoomEntity>().setSqlSelect("room_name").eq("id", memberEntity.getRoomId()));
					memberEntity.setRecPerson(room.getRoomName());
				}
			}
		}
		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("biz:member:info")
	public R info(@PathVariable("id") Long id) {
		MemberEntity member = memberService.selectById(id);

		return R.ok().put("member", member);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("biz:member:save")
	public R save(@RequestBody MemberEntity member) {
		ValidatorUtils.validateEntity(member, AddGroup.class);
		memberService.save(member);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("biz:member:update")
	public R update(@RequestBody MemberEntity member) {
		ValidatorUtils.validateEntity(member, UpdateGroup.class);
		memberService.updateById(member);

		return R.ok();
	}

	/**
	 * 认证封号
	 */
	@SysLog("认证封号")
	@RequestMapping(path = "/changeStauts", method = RequestMethod.POST)
	@RequiresPermissions("biz:member:update")
	public R changeStauts(String type, Long[] memberIds) {
		Assert.hasText(type, "参数异常[type]");
		Assert.notNull(memberIds, "memberIds is null");
		Assert.noNullElements(memberIds, "参数异常[memberIds]");
		memberService.changeStauts(type, Arrays.asList(memberIds));
		return R.ok();
	}
}
